package com.css.dzj.dzjap.modules.info.service.impl;

import com.css.dzj.dzjap.common.util.Constant;
import com.css.dzj.dzjap.modules.expertcredit.service.ExpertCreditService;
import com.css.dzj.dzjap.modules.info.entity.ApExpertEntity;
import com.css.dzj.dzjap.modules.info.entity.QApExpertEntity;
import com.css.dzj.dzjap.modules.info.mapper.ExpertMapper;
import com.css.dzj.dzjap.modules.info.service.ExpertService;
import com.querydsl.core.QueryResults;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class ExpertServieImpl implements ExpertService {
    @Autowired
    ExpertMapper expertMapper;
    @Autowired
    JPAQueryFactory jpaQueryFactory;
    @Autowired
    ExpertCreditService expertCreditService;
    @Override
    public QueryResults<ApExpertEntity> getPage(Pageable pageRequest, String expertName, String expertIdcard,
                                                String endTime, String startTime, String expertLevl,String isBlackList, String expertTel) {
        //List idCard=expertCreditService.idHbCard();
        QApExpertEntity q= QApExpertEntity.apExpertEntity;
        Predicate predicate=q.isNotNull().or(q.isNull());
        predicate = (expertName == null || expertName.isEmpty()) ? predicate : ExpressionUtils.and(predicate, q.expertName.like("%" + expertName + "%"));
        predicate = (expertIdcard == null || expertIdcard.isEmpty()) ? predicate : ExpressionUtils.and(predicate, q.expertIdcard.like("%" + expertIdcard + "%"));
        predicate=(expertLevl==null || expertLevl.isEmpty())? predicate : ExpressionUtils.and(predicate,q.expertLevl.eq(expertLevl));
        predicate=(isBlackList==null || isBlackList.isEmpty())? predicate : ExpressionUtils.and(predicate,q.isBlackList.eq(isBlackList));
        predicate=(expertTel==null || expertTel.isEmpty())? predicate : ExpressionUtils.and(predicate, q.expertTel.like("%" + expertTel + "%"));
        if((StringUtils.isEmpty(startTime))&&(!StringUtils.isEmpty(endTime))){
            Date endDate=null;
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
            try {
                endDate = sf.parse(endTime + " "+"23:59:59.999");
            } catch (ParseException e) {
                e.printStackTrace();
            }
            predicate =ExpressionUtils.and(predicate, q.workTime.loe(endDate));
        }
        if((!StringUtils.isEmpty(startTime))&&(StringUtils.isEmpty(endTime))){
            Date startDate = null;
            Date endDate=new Date();
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
            try {
                startDate = sf.parse(startTime+" "+"00:00:00.000");
            } catch (ParseException e) {
                e.printStackTrace();
            }
            predicate =ExpressionUtils.and(predicate, q.workTime.goe(startDate));
        }
        if((!StringUtils.isEmpty(startTime))&&(!StringUtils.isEmpty(endTime))){
            Date startDate = null;
            Date endDate=null;
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
            try {
                startDate = sf.parse(startTime+" "+"00:00:00.000");
                endDate = sf.parse(endTime+" "+"23:59:59.999");
            } catch (ParseException e) {
                e.printStackTrace();
            }
            predicate =ExpressionUtils.and(predicate, q.workTime.between(startDate,endDate));
        }

        return jpaQueryFactory.selectFrom(q).where(predicate,q.expertDelFlag.eq(Constant.NO_DELETE)/*,q.expertIdcard.notIn(idCard)*/).orderBy(q.expertCreatTime.desc()).offset(pageRequest.getOffset()).limit(pageRequest.getPageSize()).fetchResults();
    }


    @Override
    public boolean delExpert(String id) {
        expertMapper.deleteById(id);
        return true;
    }

    @Override
    public ApExpertEntity findById(String id) {
       ApExpertEntity apExpertEntity =expertMapper.findById(id).get();
        return apExpertEntity;
    }

    @Override
    @Transactional
    public void update(ApExpertEntity apExpertEntity) {

    }

    @Override
    @Transactional
    public void save(ApExpertEntity apExpertEntity) {
        apExpertEntity.setIsBlackList("2");
        expertMapper.save(apExpertEntity);
    }

    @Override
    public List<ApExpertEntity> listExpert(String level,String major,String unitName,String location,String provienceOutIn) {
        // List idCard=expertCreditService.idHbCard();
        QApExpertEntity qApExpertEntity = QApExpertEntity.apExpertEntity;
        Predicate predicate = qApExpertEntity.isNotNull().or(qApExpertEntity.isNull());
        predicate = ExpressionUtils.and(predicate, qApExpertEntity.isBlackList.ne("1"));
        predicate = (level==null || level.isEmpty())?predicate:ExpressionUtils.and(predicate,qApExpertEntity.expertLevl.eq(level));
        predicate = (major==null || major.isEmpty())?predicate:ExpressionUtils.and(predicate,qApExpertEntity.major.eq(major));
        predicate = (unitName==null || unitName.isEmpty())?predicate:ExpressionUtils.and(predicate,qApExpertEntity.unitName.ne(unitName));
        if (!Constant.LOCATIONZG.equals(location)){
            if(provienceOutIn==Constant.PROVIENCEIN){
                predicate = (location== null || location.isEmpty())?predicate:ExpressionUtils.and(predicate,qApExpertEntity.unitAddress.like("%"+location+"%"));
            }else if(provienceOutIn==Constant.PROVIENCEOUT){
                predicate = (location== null || location.isEmpty())?predicate:ExpressionUtils.and(predicate,qApExpertEntity.unitAddress.substring(0,2).notLike("%"+location+"%"));
            }
        }
       return jpaQueryFactory.selectFrom(qApExpertEntity)
                .where(predicate,qApExpertEntity.expertDelFlag.eq(Constant.NO_DELETE))
                .fetch();
    }

    @Override
    public ApExpertEntity findByIdCard(String idCard) {
        QApExpertEntity q = QApExpertEntity.apExpertEntity;
        return jpaQueryFactory.selectFrom(q).where(q.expertIdcard.eq(idCard)).fetchOne();
    }

    @Override
    @Transactional
    public void updateBlack(String type, String IdCard) {
        QApExpertEntity q = QApExpertEntity.apExpertEntity;
        jpaQueryFactory.update(q).set(q.isBlackList,type).where(q.expertIdcard.eq(IdCard)).execute();
    }


}
